import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2023-06-05
 * Time: 15:17
 */
public class JDBCSelect {
    public static void main(String[] args) throws SQLException {
        //1.创建并初始化数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/javamysql?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");
        //2.和数据库服务器建立连接
        Connection connection = dataSource.getConnection();
        //3.构造SQL
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4.执行SQL
        ResultSet resultSet = statement.executeQuery();
        //5.遍历结果集合
        while (resultSet.next()) {
            //把 resultSet 想象成一个表格，同时表格这里有个光标，初始情况下光标指向表最上面
            //每次调用 next，光标往下走一行,当光标已经把真个表遍历完 next就会 false
            //当光标指向某一行的时候，就可以通过 getXXX 来获取到当前这一行里的数据
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id = " + id + ", name = " + name);
        }
        //6.释放资源
        statement.close();
        connection.close();
    }
}
